package com.coocaa.ops.admin.v1.poster.provider.impl;

import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.admin.v1.poster.model.GlobalPictureModel;
import com.coocaa.ops.admin.v1.poster.service.IGlobalPictureQueryProvider;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author bjh
 * @description 素材图片表-查询provider接口实现
 * @date 2020-06-13 10:50:35
 */
@Service
public class  GlobalPictureQueryProviderImpl implements IGlobalPictureQueryProvider {
    @Autowired
    private GlobalPictureBizImpl thisBizImpl;


    /**
    * 获取详情信息
    *
    * @param id
    * @param version
    * @return
    */
    @Override
    public GlobalPictureEntity getDetailInfo(Integer id, String version){
        GlobalPictureEntity entity=this.getEntityByIdAndVersion(id,version);
        return entity;
    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<GlobalPictureEntity> pageList(GlobalPictureModel model){
        Map<String, Object> params=PageQueryUtil.getFilterMap(model);
        calculateResolutionTolerance(params, model);
        PageResultModel<GlobalPictureEntity> pageResult=thisBizImpl.getCommonPageList(params,true);
        return pageResult;
    }

    /**
     * 分页数据-其他处选择
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<GlobalPictureEntity> dataList(GlobalPictureModel model){
        Map<String, Object> params=PageQueryUtil.getFilterMap(model);
        calculateResolutionTolerance(params, model);
        PageResultModel<GlobalPictureEntity> pageResult=thisBizImpl.getCommonPageList(params,false);
        return pageResult;

    }


    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    /**
     * 获取基础信息-根据ID和版本
     *
     * @param id
     * @param version
     * @return
     */
    private GlobalPictureEntity getEntityByIdAndVersion(Integer id,String version){
       //TODO:
        return thisBizImpl.queryById(id);
    }


    /**
     * 计算容忍度相关数据
     */
    private void calculateResolutionTolerance(Map<String, Object> param, GlobalPictureModel model) {
        String resolution = model.getPictureResolution();
        //不按分辨率搜索，或按分辨率搜索但容忍度为空或为0，直接返回
        if (StringUtils.isBlank(resolution) || model.getResolutionTolerance() == null) {
            return;
        }

        String[] lengths;
        //分辨率乘号兼容*或x
        if (resolution.contains("x")) {
            lengths = resolution.split("x");
        } else if (resolution.contains("*")) {
            lengths = resolution.split("\\*");
        } else {
            return;
        }
        //获取宽高值，解析错误直接返回
        if (lengths.length != 2) {
            return;
        }
        int width, height;
        try {
            width = Integer.parseInt(lengths[0]);
            height = Integer.parseInt(lengths[1]);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return;
        }
        //宽高比最大最小值，容忍度指定为3%
        Float aspectRatioMax = 1.03f * width / height;
        Float aspectRatioMin = 0.97f * width / height;
        param.put("aspectRatioMax", aspectRatioMax);
        param.put("aspectRatioMin", aspectRatioMin);

        //宽高最大最小值，容忍度参数指定
        Integer tolerance = model.getResolutionTolerance();
        Integer widthMax = (int)(width * (1 + tolerance / 100f));
        Integer heightMax = (int)(height * (1 + tolerance / 100f));
        Integer widthMin = (int)(width * (1 - tolerance / 100f));
        Integer heightMin = (int)(height * (1 - tolerance / 100f));
        param.put("widthMax", widthMax);
        param.put("heightMax", heightMax);
        param.put("widthMin", widthMin);
        param.put("heightMin", heightMin);

    }

}
